#include "chatcommand_simplegameusergetdata.h"
#include "websocket.h"
#include "datastring.h"
#include "parameters.h"
#include "stringbuilder.h"
#include "message.h"
#include "Debug.h"
#include <inttypes.h>
#include "simplechatgame.h"

// simplegameusergetdata(messageid,gameid,username)
// Gets the data for a user in a game. Returns a simplegameuserdata message or an error message. If username is left out, returns the user data for all users.
bool chatcommand_simplegameusergetdata::processmessage(char first_letter,message *received_message,chatclient *client)
{
	if ((first_letter != 's') 
		|| (received_message->actual_message.substr(0,22)!="simplegameusergetdata(")) {
		return false;
	}	
	
	Debug debug(__FILE__,__func__,__LINE__);
	message *new_message;
	datastring method_parameters;
	parameters parameters_parsed;
	bool parameter_success = true;
	int64_t messageid;
	datastring gameid;
	datastring username;
	simplechatgame *game;
	biglist_item<simplechatgame *> *gamesearch;
	datastring error_message;	
	biglist_item<simplechatgameuser *> *usersearch;	
	simplechatgameuser *user;
	
	debug = __LINE__;
	method_parameters = received_message->actual_message.substr(22,received_message->actual_message.length-23);
	method_parameters.null_terminate(); // Used to comply with snprintf.
	parameters_parsed.long_parameter(method_parameters,parameter_success);
	parameters_parsed.string_parameter(method_parameters,parameter_success);
	parameters_parsed.string_parameter(method_parameters,parameter_success);
		
	debug = __LINE__;
	if (parameter_success) {
		// Give the parameters nice names.
		debug = __LINE__;
		messageid = parameters_parsed.long_parameters[0];
		gameid = parameters_parsed.string_parameters[1];
		username = parameters_parsed.string_parameters[2];		
	
		debug = __LINE__;
		// Look to see if this gameid is already used.
		gamesearch = the_websocket->find_simple_game(gameid);
		if (gamesearch == nullptr) {
			error_message = "That gameid was not found.";
			error(client,error_message,messageid);
			return true;
		}
		game = gamesearch->item;
		// Look for this client in this game.
		if (username.length == 0) {
			// There's no user name, so return the data for all users as separate messages.
			biglist_iterator<simplechatgameuser *> userloop(&(game->users));
			while(!userloop.eof()) {
				user = userloop.item;
				if ((user->username != nullptr) && (user->username->length > 0)) {
					new_message = simplegameuserdata(messageid,gameid,*(user->username),user->userdata == nullptr ? datastring::anemptystring() : *(user->userdata));
					client->push_message(&new_message);
				}
				userloop.movenext();
			}
		} else {
			usersearch = game->getuser(username);
			if (usersearch == nullptr) {
				error_message = "That user is not in that game.";
				error(client,error_message,messageid);
				return true;
			}
			new_message = simplegameuserdata(messageid,gameid,username,usersearch->item->userdata == nullptr ? datastring::anemptystring() : *(usersearch->item->userdata));
			if (new_message == nullptr) {
				error_message = "Out of memory.";
				error(client,error_message,messageid);
				return true;
			}
			client->push_message(&new_message);
		}
	} else {
		debug = __LINE__;
		parameters_not_correct(client,received_message);
	}
	debug = __LINE__;
	return true;
}
